<template>
  <div class="occupancy-panel">
    <div class="chart-container">
      <svg viewBox="0 0 100 100">
        <circle cx="50" cy="50" r="45" class="bg"></circle>
        <circle cx="50" cy="50" r="45" class="progress" :style="{ strokeDasharray: circumference }"></circle>
      </svg>
      <div class="percentage">{{ animatedRate }}%</div>
    </div>
    <div class="info">
      <div class="total">
        <span class="value">{{ animatedTotal }}</span>
        <span class="label">入驻总人数</span>
      </div>
      <div class="breakdown">
        <span>企业员工: {{ animatedCompany }}</span>
        <span>物业员工: {{ animatedPark }}</span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed, defineProps } from 'vue';
import { useCountUp } from '@/composables/useCountUp';

const props = defineProps({
  stats: {
    type: Object,
    required: true,
    default: () => ({ employees: { total: 0, company_employees: 0, park_employees: 0 } }),
  },
  unitData: {
    type: Array,
    required: true,
    default: () => [],
  }
});

const occupancyRate = computed(() => {
  if (!props.unitData || props.unitData.length === 0) return 0;
  
  const totalUnits = props.unitData.reduce((sum, item) => sum + item.value, 0);
  if (totalUnits === 0) return 0;

  const occupiedUnits = props.unitData
    .filter(item => ['已租', '已售', '装修中', '维修中'].includes(item.name))
    .reduce((sum, item) => sum + item.value, 0);
    
  return ((occupiedUnits / totalUnits) * 100);
});

const animatedTotal = useCountUp(() => props.stats.employees?.total || 0);
const animatedCompany = useCountUp(() => props.stats.employees?.company_employees || 0);
const animatedPark = useCountUp(() => props.stats.employees?.park_employees || 0);
const animatedRate = useCountUp(() => occupancyRate.value, 1500);

const circumference = computed(() => {
  const radius = 45;
  const circum = 2 * Math.PI * radius;
  const rate = occupancyRate.value / 100;
  const progress = rate * circum;
  return `${progress} ${circum}`;
});
</script>

<style scoped>
.occupancy-panel {
  display: flex;
  align-items: center;
  justify-content: space-around;
  height: 100%;
}
.chart-container {
  position: relative;
  width: 120px;
  height: 120px;
}
.percentage {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 28px;
  font-weight: bold;
  color: #00e5ff;
}
svg {
  transform: rotate(-90deg);
}
circle {
  fill: none;
  stroke-width: 10;
}
.bg {
  stroke: rgba(255, 255, 255, 0.1);
}
.progress {
  stroke: #00e5ff;
  stroke-linecap: round;
  transition: stroke-dasharray 0.5s ease;
}
.info {
  display: flex;
  flex-direction: column;
  gap: 15px;
  align-items: flex-start;
}
.total {
  text-align: left;
}
.value {
  font-size: 24px;
  font-weight: bold;
  color: #fff;
}
.label {
  font-size: 16px;
  color: #aed4ff;
  margin-left: 8px;
}
.breakdown {
  font-size: 14px;
  color: #aed4ff;
  display: flex;
  flex-direction: column;
  gap: 5px;
}
</style> 